package com.cjk.console.service;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.cjk.console.request.StbLogRequest;
import com.cjk.foundation.commons.BusinessException;
import com.cjk.foundation.commons.response.Response;
import com.cjk.foundation.dao.AppVersionDao;
import com.cjk.foundation.dao.ChannelDetailDao;
import com.cjk.foundation.dao.DeviceDao;
import com.cjk.foundation.dao.WatchLogDao;
import com.cjk.foundation.entity.ChannelDetailEntity;
import com.cjk.foundation.entity.DeviceEntity;
import com.cjk.foundation.entity.WatchLogEntity;
import com.cjk.foundation.enums.DeviceChangeLogType;
import com.cjk.foundation.enums.DeviceStatus;

@Service
public class AppServiceImpl implements AppService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AppServiceImpl.class); 

	@Autowired
	private DeviceDao deviceDao;
	
	@Autowired
	private ChannelDetailDao channelDao;
	
	@Autowired
	private AppVersionDao appVersionDao;
	
	@Autowired
	private WatchLogDao watchLogDao;

	@Autowired
	private LogService logService;
	
	@Transactional
	@Override
	public Response stbLog(StbLogRequest request) {
		Date date = new Date();
		DeviceEntity deviceEntity = deviceDao.selectBySn(request.getSn(), null);
		if (deviceEntity == null) {
			LOGGER.error("STB设备[{}]不存在", request.getSn());
			return Response.success();
		}
		
		switch (request.getLogType()) {
		case BOOTSTRAP:
			DeviceEntity updateBootstrap = new DeviceEntity();
			updateBootstrap.setSn(request.getSn());
			updateBootstrap.setMac(request.getMac());
			updateBootstrap.setFirstInform(request.getUploadTime());
			updateBootstrap.setDeviceClass(request.getDeviceClass());
			updateBootstrap.setRoom(request.getRoom());
			updateBootstrap.setLastUploadLogTime(date);
			updateBootstrap.setStatus(DeviceStatus.ONLINE);
			deviceDao.update(updateBootstrap);
			break;
		case PERIOD:
			DeviceEntity updatePeriod = new DeviceEntity();
			updatePeriod.setSn(request.getSn());
			updatePeriod.setTenantName(request.getTenantName());
			updatePeriod.setRoom(request.getRoom());
			updatePeriod.setLastUploadLogTime(date);
			updatePeriod.setStatus(DeviceStatus.ONLINE);
			deviceDao.update(updatePeriod);
			appVersionDao.updateVersion(request.getSn(), request.getLauncherVersion(), request.getMovieVersion(), request.getLiveVersion());
			break;
		case PLAYING :
			WatchLogEntity watchLogEntity = new WatchLogEntity();
			watchLogEntity.setSn(request.getSn());
			watchLogEntity.setUserId(deviceEntity.getUserId());
			watchLogEntity.setGroupId(deviceEntity.getGroupId());
			watchLogEntity.setRoom(request.getRoom());
			watchLogEntity.setChannelName(request.getChannel());
			watchLogEntity.setTenantName(request.getTenantName());
			watchLogEntity.setUploadTime(request.getUploadTime());
			
			DeviceEntity updatePlaying = new DeviceEntity();
			updatePlaying.setSn(request.getSn());
			updatePlaying.setTenantName(request.getTenantName());
			updatePlaying.setRoom(request.getRoom());
			updatePlaying.setLastUploadLogTime(date);
			updatePlaying.setLastPlayTime(date);
			updatePlaying.setChannelName(request.getChannel());
			
			ChannelDetailEntity channelEntity = channelDao.selectByName(deviceEntity.getGroupId(), request.getChannel(), null);
			if (channelEntity == null) {
				LOGGER.error("STB设备[{}]所播放的频道[{}]不存在", request.getSn(), request.getChannel());
			} else {
				watchLogEntity.setChannelId(channelEntity.getId());
				updatePlaying.setChannelId(channelEntity.getId());
			}
			watchLogDao.insert(watchLogEntity);
			deviceDao.update(updatePlaying);
			break;
		}
		return Response.success();
	}

	@Transactional
	@Override
	public void setTenant(String sn, String tenantName) {
		DeviceEntity deviceEntity = deviceDao.selectBySn(sn, null);
		if (deviceEntity == null) {
			throw new BusinessException("设备不存在");
		}
		deviceDao.updateTenantNameBySn(sn, tenantName);
		Date date = new Date();
		logService.insertDeviceChangeLog(sn, deviceEntity.getRoom(), tenantName, DeviceChangeLogType.TENANT, date);
	}

	@Override
	public Response getTenant(String sn) {
		DeviceEntity deviceEntity = deviceDao.selectBySn(sn, null);
		if (deviceEntity == null) {
			throw new BusinessException("设备不存在");
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("room", deviceEntity.getRoom());
		jsonObject.put("tenantName", deviceEntity.getTenantName());
		return Response.success(jsonObject);
	}

}
